/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 16:58:43
 * describe:上线
 */
APP.controller('OnlineController', ['$scope','$rootScope', 'OnlineService', '$state','PopupService',
    function ($scope,$rootScope ,OnlineService, $state,PopupService) {

        /***变量声明***/
        $scope.onlineBarcode = {
            barCode: ''
        };
        $scope.onlineData = {
            number: '',
            supplier: '',
            paper: '',
            weight: '',//克重
            specification: '',//规格
            kg: ''//重量
        };
        /**
         * 初始化函数
         */
        $scope.init = function () {
            $scope.onlineBarcode = {
                barCode: ''
            };
        };
        //选择轴序
        $scope.goSupplier = function (type) {
            $state.go('supplier', {type: type});
        };

        /**
         * 按条码查询物料信息
         */
        $scope.getByCode = function () {
            OnlineService.getByCode($scope.onlineBarcode.barCode)
                .then(function (response) {
                    console.log('成功回调方法', response);
                    if (response.data.data.state == '暂存区') {//查询状态
                        $scope.onlineData = response.data.data;
                        console.log('$scope.onlineData', $scope.onlineData);
                    } else {
                        PopupService.showToast(response.data.data.state);
                    }
                }, function (response) {
                    console.log('失败回调方法', response)
                })
        };
        /**
         * 上线操作
         */
        $scope.onLineAlex = function () {
            if(!$scope.onlineBarcode.barCode || !$rootScope.onlineAxle) {
                PopupService.showToast('请完整信息');
                return;
            }
            OnlineService.onLine($scope.onlineBarcode.barCode,$rootScope.onlineAxle)
                .success(function (response) {
                    console.log('成功回调方法', response);
                    if(response.success) {
                        PopupService.showToast('上线成功');
                        $state.go('main');
                    } else {
                        PopupService.showToast(response.message);
                    }
                }).error(function (error) {
                    console.log('失败回调方法', error.message);
            })
        };


        /*
         $scope.init();//如果页面缓存则用此方法初始化
         */
        $scope.$on('$ionicView.beforeEnter', function (e,v) {
            if (v.direction == 'back') {
                // console.log('保存前',window.localStorage.getItem('save_input'));
                console.log('前页返回');
                // console.log('v',v.direction);
                // console.log($rootScope.data);

            } else {
                $scope.init();
            }
        });
    }
]);

/**
 * creater:kunjiang.zhou@dhc.com.cn
 * create time:2017-05-31 16:58:43
 * describe:上线
 */
APP.service('OnlineService', ['$http', 'UrlService', function ($http, UrlService) {
    /**
     * 按条码查询物料信息
     */
    this.getByCode = function (data) {
        var url = 'http://47.94.130.122/api/material/querybybarcode/' + data;
        console.log(url);
        return $http.get(url);
    };
    /**
     * 上线
     */
    this.onLine = function (barCode,axle) {
        var params = {
            barcode:barCode,
            axle:axle
        };
        return $http.post(UrlService.getUrl('ON_LINE'), params);
    }

}]);
